﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MxTol.UI.Helpers;
using MxTol.Managers;
using MxTol.Models;

namespace MxTol.UI.Controllers
{
    // 学生管理
    public class StudentManagementController : Controller
    {
        private const string MIME_TYPE = "application/json;charset=utf-8;";

        #region 页面

        // 获取所有学生账号页面
        public ActionResult GetAllStudentView()
        {
            return View();
        }

        // 添加学生账号页面
        public ActionResult AddStudentView()
        {
            return View();
        }

        // 查询学生账号页面
        public ActionResult QueryStudentView()
        {
            return View();
        }

        public ActionResult UpdateStudentView()
        {
            Student student = null;
            try
            {
                string id = Request["id"];
                student = Student.GetStudentByID(new Guid(id));
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("获取需要更新的学生账号信息失败", ex);
            }

            if (student != null)
            {
                return View(student);
            }
            else
            {
                return Content("获取需要更新的学生账号信息失败");
            }
        }

        #endregion

        #region 功能

        // 获取所有学生json数据
        public ActionResult GetAllStudentJson()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string currentPage = Request["page"];
                string pageSize = Request["rows"];

                json = StudentManager.GetAllStudent(int.Parse(currentPage), int.Parse(pageSize));
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("获取所有学生Json格式数据错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        // 添加学生账号(电话做账号密码)
        public ActionResult AddStudent()
        {
            // 0：异常失败  1：电话号码已存在  2：成功
            int flag = 0;
            try
            {
                string name = Request["name"];
                string phone = Request["phone"];

                // 号码不存在
                if (!StudentManager.IsPhoneExist(phone))
                {
                    // 添加新学生账号
                    Student student = new Student();

                    student.Name = name;
                    student.Phone = phone;
                    student.Account = phone;
                    student.Psword = MD5Helper.MD5(phone);
                    student.AndroidCode = string.Empty;
                    student.IsOnline = 0;
                    student.LastLoginTime = null;
                    student.AddTime = DateTime.Now;

                    student.Save();
                    flag = 2;
                }
                else
                {
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("添加学生账号失败", ex);
            }

            return Content(flag.ToString(), MIME_TYPE);
        }

        // 查询学生账号
        public ActionResult QueryStudent()
        {
            string json = EasyuiHelper.DefaultJson;
            try
            {
                string name = Request["name"];
                string phone = Request["phone"];
                string online = Request["online"];

                // 全部搜索
                int currentPage = 1;
                string pageSize = Request["rows"];

                // 拼接sql基本查询字段
                string sql_base = "select * from Student";
                string sql_where = string.Empty;
                if (!string.IsNullOrEmpty(name))
                {
                    sql_where += string.Format("and name like '%{0}%' ", name);
                }
                if (!string.IsNullOrEmpty(phone))
                {
                    sql_where += string.Format("and phone = '{0}' ", phone);
                }
                if (online != "2")
                {
                    sql_where += string.Format("and is_online = '{0}' ", online);
                }

                // 有查询条件，没有查询所有
                string sql = sql_base;
                if (sql_where.Length != 0)
                {
                    // 去掉第一个"and"
                    if ((sql_where.TrimStart()).StartsWith("and"))
                    {
                        sql_where = sql_where.Substring(3, sql_where.Length - 3);
                        sql = sql_base + " where " + sql_where;
                    }
                }
                json = StudentManager.GetAllStudent(currentPage, int.Parse(pageSize), sql);
            }
            catch (Exception ex)
            {
                json = EasyuiHelper.DefaultJson;
                LogHelper.WriteLogAsync("查询学生账号错误", ex);
            }
            return Content(json, MIME_TYPE);
        }

        // 删除学生账号
        public ActionResult DeleteStudent()
        {
            // 0:异常失败  1：成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                Student student = Student.GetStudentByID(new Guid(id));
                if (student != null)
                {
                    student.Delete();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("删除学生账号失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 更新学生账号
        public ActionResult UpdateStudent()
        {
            // 0：异常失败  1：电话号码已存在  2：成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                string name = Request["name"];
                string phone = Request["phone"];

                // 需要更新的学生账号
                Student student = Student.GetStudentByID(new Guid(id));

                // 新电话号码
                if (phone != student.Phone)
                {
                    //新号码存在
                    if (StudentManager.IsPhoneExist(phone))
                    {
                        return Content("1", MIME_TYPE);
                    }
                }

                // 更新
                student.Name = name;
                student.Phone = phone;
                student.Account = phone;
                student.Psword = MD5Helper.MD5(phone);

                student.Update();
                flag = 2;
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("更新学生账号失败", ex);
            }

            return Content(flag.ToString(), MIME_TYPE);
        }

        // 密码重置(密码为电话号码)
        public ActionResult ResetPsword()
        {
            // 0：异常失败  1：成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                Student student = Student.GetStudentByID(new Guid(id));
                if (student != null)
                {
                    student.Psword = MD5Helper.MD5(student.Phone);
                    student.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("重置学生账号密码失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        // 手机解绑
        public ActionResult ClearAndroidCode()
        {
            // 0：异常失败  1：成功
            int flag = 0;
            try
            {
                string id = Request["id"];
                Student student = Student.GetStudentByID(new Guid(id));
                if (student != null)
                {
                    student.AndroidCode = string.Empty;
                    student.Save();
                    flag = 1;
                }
            }
            catch (Exception ex)
            {
                flag = 0;
                LogHelper.WriteLogAsync("重置学生安卓客户端机器码失败", ex);
            }
            return Content(flag.ToString(), MIME_TYPE);
        }

        #endregion
    }
}
